/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-05 16:29:00
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanMerchant;
import zyy.model.BeanProduct;
import zyy.model.BeanProduct_Dir;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmAdminMerchant extends JDialog implements ActionListener {
  private static final long serialVersionUID = -990815448731736703L;
  // 商家
  private BeanMerchant currentMerchant = null;
  private Object tblMerchantTitle[] = BeanMerchant.tableTitles;
  private Object tblMerchantData[][];
  DefaultTableModel tabMerchantModel = new DefaultTableModel();
  private JTable dataTableMerchant = new JTable(tabMerchantModel);
  List<BeanMerchant> allMerchant = null;
  // 商品目录
  private BeanProduct_Dir currentProduct_Dir = null;
  private Object tblProduct_DirTitle[] = BeanProduct_Dir.tableTitles;
  private Object tblProduct_DirData[][];
  DefaultTableModel tabProduct_DirModel = new DefaultTableModel();
  private JTable dataTableProduct_Dir = new JTable(tabProduct_DirModel);
  List<BeanProduct_Dir> allProduct_Dir = null;
  // 商品信息
  private BeanProduct currentProduct = null;
  private Object tblProductTitles[] = BeanProduct.tableTitles;
  private Object tblProductData[][];
  DefaultTableModel tabProductModel = new DefaultTableModel();
  private JTable dataTableProduct = new JTable(tabProductModel);
  List<BeanProduct> allProduct = null;

  // Panel 布局
  private JPanel mdpTblPanel = new JPanel();
  // private JPanel orderInfoTablePanel = new JPanel();
  private JPanel toolBarPanel = new JPanel();

  private JButton exit = new JButton("退出");
  private JButton addM = new JButton("添加商家");
  private JButton addPD = new JButton("添加分类");
  private JButton addP = new JButton("添加商品");
  private JButton delM = new JButton("删除商家");
  private JButton delPD = new JButton("删除分类");
  private JButton delP = new JButton("删除商品");

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.exit) {
      this.setVisible(false);
    } else if (e.getSource() == this.addM) {
      System.out.println("管理员 添加商家");
      FrmNewMerchant fNewMerchant = new FrmNewMerchant();
      Bus.fNewMerchant = fNewMerchant;
    } else if (e.getSource() == this.addPD) {
      if (this.getCurrentMerchant() == null) {
        JOptionPane.showMessageDialog(null, "请先选中商品目录的宿主商家", "错误", JOptionPane.ERROR_MESSAGE);
      }
      new FrmNewPD();
    } else if (e.getSource() == this.addP) {
      if (this.getCurrentMerchant() == null) {
        JOptionPane.showMessageDialog(null, "请先选中商品的\n宿主商家", "错误", JOptionPane.ERROR_MESSAGE);
      } else if (this.currentProduct_Dir == null) {
        JOptionPane.showMessageDialog(null, "请先选中商品的\n宿主商品分类", "错误", JOptionPane.ERROR_MESSAGE);
      } else {
        System.out.println("管理员 添加商品");
        new FrmNewProduct();
      }
    } else if (e.getSource() == this.delM) {
      if (this.getCurrentMerchant() == null) {
        JOptionPane.showMessageDialog(null, "请先选中想删除的商家", "错误", JOptionPane.ERROR_MESSAGE);
      }
      System.out.printf("管理员 移除商家 %d:%s", this.getCurrentMerchant().getMID(), this.getCurrentMerchant().getmName());
      try {
        Bus.merchantManager.unadd(this.getCurrentMerchant().getMID());
        this.reloadMerchantTable();
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    } else if (e.getSource() == this.delPD) {
      System.out.println("管理员 移除商品分类");
      if (this.currentProduct_Dir == null) {
        JOptionPane.showMessageDialog(null, "请选中要删除的商品分类", "错误", JOptionPane.ERROR_MESSAGE);
      } else if (this.currentProduct_Dir.getPdCnt() > 0) {
        JOptionPane.showMessageDialog(null, "不可删除\n商品分类内仍有商品", "错误", JOptionPane.ERROR_MESSAGE);
      } else
        try {
          Bus.Product_DirManager.unadd(this.currentProduct_Dir.getProductDirID());
          this.reloadProduct_DirTable();
        } catch (BusinessException ex) {
          JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
          ex.printStackTrace();
        }
    } else if (e.getSource() == this.delP) {
      if (this.currentProduct == null) {
        JOptionPane.showMessageDialog(null, "请选中要删除的商品", "错误", JOptionPane.ERROR_MESSAGE);
      } else {
        System.out.println("管理员 移除商品");
        try {
          Bus.ProductManager.unadd(this.currentProduct);
          this.reloadProduct_DirTable();
          this.reloadProductTable();
        } catch (BusinessException ex) {
          JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
          ex.printStackTrace();
        }
      }
    }
  }

  public BeanMerchant getCurrentMerchant() {
    return currentMerchant;
  }

  public void setCurrentMerchant(BeanMerchant currentMerchant) {
    this.currentMerchant = currentMerchant;
    this.reloadProduct_DirTable();
  }

  public void reloadMerchantTable() {
    try {
      allMerchant = Bus.merchantManager.loadALL();
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblMerchantData = new Object[allMerchant.size()][BeanMerchant.tableTitles.length];
    for (int i = 0; i < allMerchant.size(); i++) {
      for (int j = 0; j < BeanMerchant.tableTitles.length; j++)
        tblMerchantData[i][j] = allMerchant.get(i).getCell(j);
    }
    tabMerchantModel.setDataVector(tblMerchantData, tblMerchantTitle);
    this.dataTableMerchant.validate();
    this.dataTableMerchant.repaint();
  }

  public void reloadProduct_DirTable() {
    if (FrmAdminMerchant.this.getCurrentMerchant() == null) {
      JOptionPane.showMessageDialog(null, "请选择一个商家", "错误", JOptionPane.ERROR_MESSAGE);
    }
    try {
      allProduct_Dir = Bus.Product_DirManager.loadByMID(FrmAdminMerchant.this.getCurrentMerchant().getMID());
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblProduct_DirData = new Object[allProduct_Dir.size()][BeanProduct_Dir.tableTitles.length];
    for (int i = 0; i < allProduct_Dir.size(); i++) {
      for (int j = 0; j < BeanProduct_Dir.tableTitles.length; j++)
        tblProduct_DirData[i][j] = allProduct_Dir.get(i).getCell(j);
    }
    tabProduct_DirModel.setDataVector(tblProduct_DirData, tblProduct_DirTitle);
    this.dataTableProduct_Dir.validate();
    this.dataTableProduct_Dir.repaint();
  }

  public void reloadProductTable() {
    if (FrmAdminMerchant.this.currentProduct_Dir == null) {
      JOptionPane.showMessageDialog(null, "请选择一个商品类别", "错误", JOptionPane.ERROR_MESSAGE);
    }
    try {
      allProduct = Bus.ProductManager.loadByProduct_DirID(FrmAdminMerchant.this.currentProduct_Dir.getProductDirID());
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblProductData = new Object[allProduct.size()][BeanProduct.tableTitles.length];
    for (int i = 0; i < allProduct.size(); i++) {
      for (int j = 0; j < BeanProduct.tableTitles.length; j++)
        tblProductData[i][j] = allProduct.get(i).getCell(j);
    }
    tabProductModel.setDataVector(tblProductData, tblProductTitles);
    this.dataTableProduct.validate();
    this.dataTableProduct.repaint();
  }

  public FrmAdminMerchant() {
    {
      this.setTitle("外卖助手 管理员 商家管理");
      this.setSize(1318, 838);
      this.setLocationRelativeTo(null);
      // this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
      this.setVisible(true);
    }
    {
      // body 布局
      mdpTblPanel.setLayout(new BorderLayout());// 如果这里不设置的话，商品信息表将无法展示
      this.getContentPane().add(this.mdpTblPanel, BorderLayout.NORTH);
      this.getContentPane().add(this.toolBarPanel, BorderLayout.SOUTH);
    }
    {
      // toolBarPanel 设置
      toolBarPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
      toolBarPanel.add(this.addM);
      toolBarPanel.add(this.addPD);
      toolBarPanel.add(this.addP);
      toolBarPanel.add(this.delM);
      toolBarPanel.add(this.delPD);
      toolBarPanel.add(this.delP);
      toolBarPanel.add(this.exit);
      Bus.jButtonZYYStyle(this.exit);
      Bus.jButtonZYYStyle(this.addM);
      Bus.jButtonZYYStyle(this.addPD);
      Bus.jButtonZYYStyle(this.addP);
      Bus.jButtonZYYStyle(this.delM);
      Bus.jButtonZYYStyle(this.delPD);
      Bus.jButtonZYYStyle(this.delP);
      this.exit.addActionListener(this);
      this.addM.addActionListener(this);
      this.addPD.addActionListener(this);
      this.addP.addActionListener(this);
      this.delM.addActionListener(this);
      this.delPD.addActionListener(this);
      this.delP.addActionListener(this);
    }
    {
      // 商家信息表
      this.mdpTblPanel.add(new JScrollPane(this.dataTableMerchant), BorderLayout.WEST);
      this.dataTableMerchant.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmAdminMerchant.this.dataTableMerchant.getSelectedRow();
          if (i < 0) {
            return;
          }
          {
            // add for 商品分类
            FrmAdminMerchant.this.setCurrentMerchant(FrmAdminMerchant.this.allMerchant.get(i));
            // FrmAdminMerchant.this.reloadProduct_DirTable();
            // 每次刷新都顺便增加默认选择
            if (FrmAdminMerchant.this.allProduct_Dir.size() > 0) {
              FrmAdminMerchant.this.currentProduct_Dir = FrmAdminMerchant.this.allProduct_Dir.get(0);
              FrmAdminMerchant.this.reloadProductTable();
            }
            if (FrmAdminMerchant.this.allProduct.size() > 0)
              FrmAdminMerchant.this.currentProduct = FrmAdminMerchant.this.allProduct.get(0);
          }
          {
            // Debug OK
            System.out.println("allMerchant.size is " + FrmAdminMerchant.this.allMerchant.size());
            System.out.println(FrmAdminMerchant.this.getCurrentMerchant());
          }
        }
      });
    }
    {
      // 商品分类表
      this.mdpTblPanel.add(new JScrollPane(this.dataTableProduct_Dir), BorderLayout.CENTER);
      this.dataTableProduct_Dir.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmAdminMerchant.this.dataTableProduct_Dir.getSelectedRow();
          if (i < 0) {
            return;
          }
          {
            // add for 商品信息
            FrmAdminMerchant.this.currentProduct_Dir = FrmAdminMerchant.this.allProduct_Dir.get(i);
            FrmAdminMerchant.this.reloadProductTable();
          }
          {
            // OK
            System.out.println(FrmAdminMerchant.this.currentProduct_Dir);
            System.out.println("allProduct_Dir.size is " + FrmAdminMerchant.this.allProduct_Dir.size());
          }
        }
      });
    }
    {
      // 商品信息表
      this.mdpTblPanel.add(new JScrollPane(this.dataTableProduct), BorderLayout.EAST);
      this.dataTableProduct.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmAdminMerchant.this.dataTableProduct.getSelectedRow();
          if (i < 0) {
            return;
          }
          FrmAdminMerchant.this.currentProduct = FrmAdminMerchant.this.allProduct.get(i);
          System.out.println(FrmAdminMerchant.this.currentProduct); // OK
        }
      });
    }
    {
      // 初始化选择
      this.reloadMerchantTable();
      this.setCurrentMerchant(FrmAdminMerchant.this.allMerchant.get(0));
      this.reloadProduct_DirTable();
      this.currentProduct_Dir = FrmAdminMerchant.this.allProduct_Dir.get(0);
      this.reloadProductTable();
      this.currentProduct = FrmAdminMerchant.this.allProduct.get(0);
    }
  }

  public BeanProduct_Dir getCurrentProduct_Dir() {
    return currentProduct_Dir;
  }

  public void setCurrentProduct_Dir(BeanProduct_Dir currentProduct_Dir) {
    this.currentProduct_Dir = currentProduct_Dir;
  }
}